package org.hales.security;

import org.hales.common.Enum;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.hales.common.ServerResponse;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author : Yanml
 * @date : 2022/11/4 14:24
 */
@RestController
public class SecurityController {

    @GetMapping("/authentication/require")
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public void requireAuthentication( HttpServletResponse response) throws IOException {
        Object obj = ServerResponse.createByErrorCodeMessage(Enum.ResponseCode.NEED_LOGIN.getCode(), Enum.ResponseCode.NEED_LOGIN.getDesc());
        ObjectMapper objMapper = new ObjectMapper();
        String result = objMapper.writeValueAsString(obj);
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter writer = response.getWriter();
        writer.print(result);
        writer.close();
        response.flushBuffer();
    }


}
